package com.wang.rabbitmqstudy.web;

import com.wang.rabbitmqstudy.config.PropertiesConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 参数示例
 */
@Api(tags = "参数示例")
@RequestMapping("/param")
@RestController
public class ParamController {
    @Autowired
    private RabbitMessagingTemplate rabbitMessagingTemplate;
    @Autowired
    private PropertiesConfig propertiesConfig;

    @ApiOperation(value = "排他队列")
    @GetMapping("/exclusiveQueue")
    public String exclusiveQueue() {
        String payload = "exclusiveQueue";
        rabbitMessagingTemplate.convertAndSend(propertiesConfig.getArgumentsExchange(), propertiesConfig.getArgumentsExclusiveRoutingKey(), payload);
        return "exclusiveQueue done";
    }

    @ApiOperation(value = "自动删除队列")
    @GetMapping("/autoDeleteQueue")
    public String autoDeleteQueue() {
        String payload = "autoDeleteQueue";
        rabbitMessagingTemplate.convertAndSend(propertiesConfig.getArgumentsExchange(), propertiesConfig.getArgumentsAutoDeleteRoutingKey(), payload);
        return "autoDeleteQueue done";
    }

    @ApiOperation(value = "队列消息自动过期")
    @GetMapping("/ttlQueueMessage")
    public String ttlQueueMessage() {
        // 消息10s后自动过期
        Map<String, Object> headers = new HashMap<>();
        headers.put("x-message-ttl", 10000);
        String payload = "ttlQueueMessage";
        rabbitMessagingTemplate.convertAndSend(propertiesConfig.getArgumentsExchange(), propertiesConfig.getRedRoutingKey(), payload, headers);
        return "ttlQueueMessage done";
    }

    @ApiOperation(value = "优先级队列")
    @GetMapping("/priorityQueue")
    public String priorityQueue() {
        String payloadLow = "priority";
        for (int i = 0; i < 200; i++) {
            Map<String, Object> headers = new HashMap<>();
            headers.put("priority", i);
            rabbitMessagingTemplate.convertAndSend(propertiesConfig.getArgumentsExchange(), propertiesConfig.getArgumentsPriorityRoutingKey(), payloadLow, headers);
        }
        return "priorityQueue done";
    }

    /**
     * 队列设置x-message-ttl、消息设置expiration，两者同时生效取较小值
     * x-message-ttl过期的数据都在队首，实时变更。expiration则不确定需要扫描整个队列，所以可能不是实时的
     */
    @ApiOperation(value = "ttl队列")
    @GetMapping("/ttlQueue")
    public String ttlQueue() {
        String payloadLow = "ttlQueue";
        Map<String,Object> headers = new HashMap<>();
        headers.put("expiration",5000);
        rabbitMessagingTemplate.convertAndSend(propertiesConfig.getArgumentsExchange(), propertiesConfig.getArgumentsTtlRoutingKey(), payloadLow);
        return "ttlQueue done";
    }

    /**
     * 1.消费(reject || nack) && requeue=false
     * 2.消息过期
     * 3.队列消息过多，队首进入死信队列
     */
    @ApiOperation(value = "死信队列")
    @GetMapping("/deadLetterQueue")
    public String deadLetterQueue() {
        String payloadLow = "deadLetterMessage";
        rabbitMessagingTemplate.convertAndSend(propertiesConfig.getArgumentsExchange(), propertiesConfig.getArgumentsCommonRoutingKey(), payloadLow);
        return "deadLetterMessage done";
    }

    /**
     * 备用交换机测试，通过routing key找不到queue，会发送到备用交换机上
     */
    @ApiOperation(value = "备用交换机")
    @GetMapping("/backup")
    public String backup() {
        String payloadLow = "backup";
        // 正常消息
        rabbitMessagingTemplate.convertAndSend(propertiesConfig.getBackupCommonExchange(), propertiesConfig.getBackupCommonRoutingKey(), payloadLow);
        // 无法路由到queue的消息
        rabbitMessagingTemplate.convertAndSend(propertiesConfig.getBackupCommonExchange(), "test123", payloadLow);
        return "backup done";
    }

}
